<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Mihail Markitan
 * Date: 10/30/11
 * Time: 9:02 PM
 */
?>
<?php

 ?>

<?php 
 


if(($model->isEditable() || InvoiceUnlocker::isInvoiceUnlocked($model->invoice))){
    
    $editableValue='true';
}else{
    $editableValue='false';
}
$hold = !!$model->hold;
$invoiceAttributes = json_encode($model->attributes);
if($hold){
	$hold  ='true';
	}else $hold = 'false';
Yii::app()->clientScript->registerScript('invoicetypetype', "
        var type='$type';
        var editable=$editableValue;
           var hold=$hold;
        var invoiceModel = $invoiceAttributes;
        ", CClientScript::POS_HEAD); ?>

<?php Yii::app()->clientScript->registerScript('itemPriceCode', "itemPriceCode='{$model->pricecode}'", CClientScript::POS_HEAD); ?>
<?php Yii::app()->clientScript->registerScript('custno', "var custno='$model->custno';", CClientScript::POS_HEAD); ?>
<?php Yii::app()->clientScript->registerScript('invoice', "invoice='$model->invoice'; ", CClientScript::POS_HEAD); ?>
<?php Yii::app()->clientScript->registerScriptFile('js/jquery.inlineedit.js', CClientScript::POS_HEAD); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/invoicetechnician.js', CClientScript::POS_BEGIN); ?>
<?php
	Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/restrictiveFileUpload.js', CClientScript::POS_BEGIN);
    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/invoicesItems.js', CClientScript::POS_BEGIN);
	Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/invoicesTransactions.js', CClientScript::POS_BEGIN);   
?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/invoicesFunctions.js', CClientScript::POS_BEGIN); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/invoiceupdate.js', CClientScript::POS_BEGIN); ?>
<div id="left-col">
<?php
$this->renderPartial('_side', array(
    'location' => $model->location,
    'customer' => $model->location->customer,
    'action' => 'update',
    'model' => $model,
    'type' => $type
));
?>

</div>

<div id="right-col">
<?php



?>
    <div class="wrap">
        <div class="container">


            <div id="breadcrumb" style="display:none;"><span style="padding-left:10px;font-size:10px;color:#777777;" id="alsoviewing"></span></div>
            <div class="inside">
<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'invoice-form',
    'enableAjaxValidation' => true,
        ));
?>
                <h1>Update <?= $type ?>: <?= $model->invoice ?> 

                <?php if ($model->invoicetype == Invoice::TYPE_INVOICE && $model->checkPaid()): ?>
                        <img src="images/paid.png" alt="Paid"/>
                    <?php endif ?>
                    <?php if ($model->invoicetype == Invoice::TYPE_QUOTE): ?>
                        <?php if ($model->quotestatus == Invoice::STATUS_APPROVED): ?>
                            <img src="images/approved.png" alt="Approved"/>
                        <?php elseif ($model->quotestatus == Invoice::STATUS_REJECTED): ?>
                            <img src="images/rejected.png" alt="Rejected"/>
                        <?php endif ?>
                    <?php endif ?>

                </h1>
                <br/>
<?php
if ($model->invoicetype == Invoice::TYPE_CREDIT_MEMO) {
    $link=CHtml::link($model->invoiceorg,array('update','invoice'=>$model->invoiceorg));
    echo CHtml::tag('b', array(), 'Credit Memo for Invoice No: ' .$link);
}
if ($model->invoicetype == Invoice::TYPE_SALES_ORDERS) {
    echo CHtml::tag('b', array(), 'Quote Reference: ' . $model->quoteorg);
    echo '<br/>';
}
if ($model->invoicetype == Invoice::TYPE_INVOICE) {
    echo CHtml::tag('b', array(), 'Sales Order Reference: ' . $model->salesordersorg);
    echo '<br/>';
}
?>

                <div class="form">



                    <div class="row" style="height:80px">
                        <table style=" width: 755px;">
                            <tr>
                                <td style=" width: 363px;">
<?php echo $form->labelEx($model, 'billloc', array('style' => 'width: 100px;')); ?>
                                    <br clear="all"/>
                                    <?php
                                    $customer = Customers::model()->findByPk($model->custno);

                                    $data = $model->getBillToList($model->location->custno);
                                    echo $form->dropDownList($model, 'billloc', $data);
                                    ?>

                                    <?php echo $form->error($model, 'billloc'); ?> 
                                    <br clear="all"/>
                                    <span id='billadd1'></span><br/>
                                    <span id='billadd2'></span><br/>
                                    <span id='billcsz'></span><br/>
                                </td>
                                <td >

                                    <label>Ship To</label>

                                    <br clear="all"/>
                                    <div class="model-data">

                                        <span class="shipname"><?= $model->shipname ?></span>
<?php echo $form->textField($model, 'shipname', array('style' => 'display:none;')); ?>
                                    </div>
                                    <br clear="all"/>

                                    <span class="shipAddress1"><?= $model->shipaddress1 ?></span>
<?php echo $form->textField($model, 'shipaddress1', array('style' => 'display:none;')); ?>
                                    <br/>
                                    <span class="shipAddress2"><?= $model->shipaddress2 ?></span>
                                    <?php echo $form->textField($model, 'shipaddress2', array('style' => 'display:none;')); ?>
                                    <br/>
                                    <span class="shipCSZ"><?= $model->shipCSZ ?></span>
                                    <?php echo $form->textField($model, 'shipCSZ', array('style' => 'display:none;')); ?>
                                    <br/>
                                </td>

                            </tr>
                        </table>

                    </div>
                    <br clear="both"/>  <br clear="both"/> <br clear="both"/>
                    <div class="left-column">
                        <div class="row short">
                            <label><?= $type ?> Number</label>
<?php echo $form->textField($model, 'invoice'); ?>
<?php echo $form->error($model, 'invoice'); ?>


                        </div>
                        <div class="row short">
                            <label><?= $type ?> Date</label>

<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model' => $model,
    'attribute' => 'invoicedate',
    'value' => CTimestamp::formatDate('Y-m-d', $model->invoicedate),
    'options' => array('dateFormat' => 'yy-mm-dd'),
));
?>
                            <?php echo $form->error($model, 'invoicedate'); ?>
                        </div>
                        <div class="row short" style="white-space: nowrap;">
                            <?php echo $form->labelEx($model, 'department'); ?>
<?php echo $form->dropDownList($model, 'department', $model->getDepartments()); ?>
                            <?php echo $form->error($model, 'department'); ?>
                        </div>
                        <div class="row short" style="white-space: nowrap;">
							
                           
								 <?php $ifExempt = $model->getLocation()->isexempt ==Locations::TAX_EXEMPT_YES?>
								<?php if($model->version == 1):?>
                         <label>  Tax Rate: </label> 
                            
                          <div class="model-data" id="taxData">
                         <?=$model->getTaxModel()->state?> <?=$model->getTaxModel()->salestaxrate ?> %
                         <?php if($ifExempt): ?>
                                 Location is exempt
                                    <?php endif; ?>
     </div>
                               <?php else:?>
                                 <?php if(!$model->checkNex()): ?>
                                  <label>  Tax Rate: </label> 
                                    <div class="model-data" id='taxData'>
                                   <?php echo $model->getTaxModelByZip($model->getLocation()->zip)->state ;?> 
                                 <?=$model->getTaxModelByZip($model->getLocation()->zip)->salestaxrate?> %<br/>
                                 <?php if($ifExempt): ?>
                                Location is exempt
                                    <?php endif; ?>
                                 </div>
                                  <?php else :?>
                                 <label>Nex Tax Rate: </label> 
                                   <div class="model-data">  <?=$model->getTaxModel()->state?> <?=$model->getTaxModel()->salestaxrate ?> %  
                                <?php if($ifExempt): ?>
                             >   Location is exempt
                                    <?php endif; ?>
                                   </div>
                                <?php endif;?>
                               <?php endif;?>
                             
                         


                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'jobnumber'); ?>
                            <?php echo $form->dropDownList($model, 'jobnumber', Jobs::getJobByLocation($model->custno, $model->locno)); ?>
                            <?php echo $form->error($model, 'jobnumber') ?>

                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'ponumber'); ?>
                            <?php echo $form->textField($model, 'ponumber'); ?>
                            <?php echo $form->error($model, 'ponumber'); ?>

                            <?php //echo CHtml::submitButton($model->isNewRecord ? 'Create Invoice' : 'Save', array('align' => 'left')); ?>

                        </div>
                          <div class="row short">
                            <?php echo $form->labelEx($model, 'version'); ?>
                            <div class="model-data">
								<?=$model->version?>
                            </div>
                            <?php echo $form->error($model, 'version'); ?>

                           

                        </div>
                        <div class="row short" id="dispatch" style="display:none;">
                            <?php echo $form->labelEx($model,'dispatch');?>
                            <?php echo $form->textField($model,'dispatch');?>
                            <?php echo $form->error($model,'dispatch');?>
                        </div>
                    </div>
                    <div class="right-column">
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'agreementno'); ?>
                            <?php if($model->agreementno==null):?>
                            <?php echo $form->dropDownList($model, 'agreementno', Contracts::getLocalList($model->custno, $model->locno)); ?>
                            <?php else:?>
                            <div class='model-data'><?php echo $model->agreementno; ?></div>
                            <?php endif; ?>
                            <?php echo $form->error($model, 'agreementno'); ?>
                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'pricecode'); ?>
                            <div class='model-data'><?php echo $model->pricecode; ?></div>
                            <?php echo $form->error($model, 'pricecode'); ?>
                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'salesperson'); ?>
<?php
$data = Customers::model()->getSalePersonList();
echo $form->dropDownList($model, 'salesperson', $data);
?>
                            <?php echo $form->error($model, 'salesperson'); ?>
                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'clerk'); ?>
                            <div class="model-data">
                            <?php echo Employees::getEmployeeName($model->clerk); ?>
                            </div>

                        </div>
                        <div class="row short">
<?php echo $form->labelEx($model, 'slterms'); ?>
<?php echo $form->dropDownList($model, 'slterms', Customers::model()->getTermList()); ?>
                            <?php echo $form->error($model, 'slterms'); ?>
                        </div>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'purchasedby'); ?>
<?php echo $form->textField($model, 'purchasedby'); ?>
                            <?php echo $form->error($model, 'purchasedby'); ?>
                        </div>
                            <?php if ($model->invoicetype == Invoice::TYPE_QUOTE): ?>
                            <div class="row short">
                                <label><?= $type ?> status</label>
                            <?php echo $form->dropDownList($model, 'quotestatus', $model->getQuoteStatusList()); ?>
    <?php echo $form->error($model, 'quotestatus'); ?>
                            </div>

                                <?php
                                $style = $model->quotestatus != Invoice::STATUS_REJECTED ? 'display:none' : NULL;
                                ?>
                            <div id="rejectedreason" style="<?= $style ?>">

                                <div class="row short">
    <?php echo $form->labelEx($model, 'rejectedreason'); ?>
    <?php echo $form->textArea($model, 'rejectedreason', array('style' => 'width:500px;')); ?>
                                    <?php echo $form->error($model, 'rejectedreason'); ?>
                                </div>

                            </div>
<?php elseif ($model->invoicetype == Invoice::TYPE_SALES_ORDERS): ?>
                            <div class="row short">
                                <label>Sales Order status</label>
                            <?php echo $form->dropDownList($model, 'salesorderstatus', JobsStatus::getListData()); ?>
    <?php echo $form->error($model, 'salesorderstatus'); ?>
                            </div>
                            <?php endif ?>

                    </div>



                    <br clear="both"/>

                </div>
                
                <table style="width:700px">
                    <tr>
                        <td>Dispatch</td>
                        <td><?= $model->dispatch ?></td>
                        <td>Discount</td>
                        <td>0.00</td>
                        <td>Balance</td>
                        <td>0.00</td>
                    </tr>
                    <tr>
                        <td>Regular Hours</td>
                        <td id="regularhours"><?= InvoiceEmp::model()->getTotalRegularHours($model->invoice) ?></td>
                        <td></td>
                        <td></td>
                        <td>Over 30</td>
                        <td>0.00</td>              
                    </tr>
                    <tr>
                        <td>Overtime</td>
                        <td id="overhead"><?= InvoiceEmp::model()->getTotalRegularHours($model->invoice) ?></td>
                        <td></td>
                        <td></td>
                        <td>Over 60</td>
                        <td>0.00</td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>Over 90</td>
                        <td>0.00</td>
                    </tr>
                </table>

                <br/>
                <ul id="tab" class="tabs tabs-with-summary">
                    <li class="active"><a href="#invoice"><?= $type ?></a></li>
                    <li><a href="#technician">Technician</a></li>
                     <li><a href="#transactions">Transactions</a></li>
                    <li><a href="#equipment" class='unavailable'>Equipment</a></li>
                    <li><a href="#document" class='unavailable'>Document</a></li>
                </ul>

                <br/>   
                <div id="invoice">
                    <a href="javascript:void(0)" id="createItem" class="add-button">Add Item</a>
                    <br/>
                    <?php 
                    echo CHtml::label('Discount', 'discount').'  ';
                    if($model->discounted) echo 'Enabled';
                        else
                    echo CHtml::dropDownList('discount', '0', array(0=>'No',1=>'Yes'),array('id'=>'inv_discount')); ?>
                    <br/>
                    <div id="item-list">

                    </div>
                </div>
                <div id="technician">
                    <a href="javascript:void(0)" id="createTechnician" class="add-button">Add Technician</a>
                    <br/>
                    <div id="emp-list"></div>
                </div>
                   <div id="transactions">
                    <div id="trans-list">

                    </div>

                </div>
                <div id="equipment"></div>
                <div id="document"></div>
                <br/>

                <div class="form">
                    <div class="left-column" style="">
<?php if ($model->invoicetype == INVOICE::TYPE_INVOICE): ?>
                        <div class="row short">
                            <?php echo $form->labelEx($model, 'hold'); ?>
                            <?php echo $form->checkBox($model, 'hold', array('value'=>-1,'uncheckValue'=>0)); ?>
                            <?php echo $form->error($model, 'hold'); ?>
                            </div>
                           

 <div class="row short">
                            <?php echo $form->labelEx($model, 'postedQuickBook'); ?>
                            <?php echo $form->dropDownList($model, 'postedQuickBook',Invoice::model()->getPostedQuickBookList()); ?>
                            <?php echo $form->error($model, 'postedQuickBook'); ?>
                        </div>

 <?php endif ?>
                    </div>

                    <div class="right-column" style="float:right;">

<?php
$this->renderPartial('_summary', array(
    'invoice' => $model
));
?>

                    </div>

                </div>
                <?php echo $form->hiddenField($model,'discounted'); ?>
                <?php $this->endWidget('CActiveForm'); ?>
            </div><!-- inside -->
        </div><!-- container -->
    </div><!-- wrap -->
    <?php $this->renderPartial('/copyright'); ?>
</div> <!-- left-col -->
